﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DCTAccess.Business;
using DCTLib.Common;
using System.Text.RegularExpressions;
using DCTAccess.Business.Administrator;

public partial class AdminZone_AdminRegister : System.Web.UI.Page
{
    #region Properties
    private string Message
    {
        get { return lblError.Text; }
        set { lblError.Text = value; }
    }
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        Message = String.Empty;
        if (!IsPostBack)
        {
            ClearInputValues();
        }
    }

    protected void btnRegister_Click(object sender, EventArgs e)
    {
        if (ValidateData())
        {
            Administrator adminChecking = AdministratorController.GetAdministratorByUsername(txtUsername.Text);
            if (adminChecking == null)
            {
                Administrator admin = GetAdministrator();
                //Message = AdministratorController.Insert(admin).ToString();
                if (AdministratorController.Insert(admin))
                {
                    Message = AppMessages.CREATE_SUCCESSFULLY;
                    Session["AdminRegister"] = AppMessages.CREATE_SUCCESSFULLY;
                    Response.Redirect(AppUrls.AdminZone.HOMEPAGE);
                }
                else
                {
                    Message = AppMessages.CANNOT_CREATE;
                }
            }
            else
            {
                Message = AppMessages.AlreadyExist("This user");
            }
        }
    }

    protected void btnClear_Click(object sender, EventArgs e)
    {
        txtUsername.Text = String.Empty;
        txtPassword.Text = String.Empty;
        txtConfirmPassword.Text = String.Empty;
        txtFullName.Text = String.Empty;
    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        //Nothing
    }

    #region Private Methods

    private void ClearInputValues()
    {
        txtUsername.Text = string.Empty;
        txtPassword.Text = string.Empty;
        txtFullName.Text = string.Empty;
        txtConfirmPassword.Text = string.Empty;
        ddlRole.SelectedIndex = 0;
    }

    private bool ValidateData()
    {
        bool validated = true;

        if (ValidationHelper.IsEmpty(txtUsername))
        {
            Message += AppMessages.RequireField("Username") + "<br/>";
            validated = false;
        }
        else
        {
            Regex regex = new Regex("^[a-zA-Z_0-9\\s]+$", RegexOptions.Compiled | RegexOptions.Singleline);
            if (!regex.Match(txtUsername.Text).Success)
            {
                Message += AppMessages.InvalidFormat("Username") + "<br/>";
                validated = false;
            }
        }
        if (ValidationHelper.IsEmpty(txtPassword))
        {
            Message += AppMessages.RequireField("Password") + "<br/>";
            validated = false;
        }
        if (ValidationHelper.IsEmpty(txtConfirmPassword))
        {
            Message += AppMessages.RequireField("Confirm Password") + "<br>";
            validated = false;
        }
        else if (txtPassword.Text != txtConfirmPassword.Text)
        {
            Message += AppMessages.MustMatch("Password", "Confirm Password") + "<br>";
            validated = false;
        }
        if (ValidationHelper.IsEmpty(txtFullName))
        {
            Message += AppMessages.RequireField("Full Name") + "<br>";
            validated = false;
        }

        if (this.ddlRole.SelectedIndex == 0)
        {
            Message += AppMessages.RequireField("Role") + "<br>";
            validated = false;
        }

        return validated;
    }

    private Administrator GetAdministrator()
    {
        Administrator admin = new Administrator();
        admin.FullName = txtFullName.Text;
        admin.HashedPassword = MD5Helper.GetMD5Hash(txtPassword.Text);
        admin.UserName = txtUsername.Text;
        admin.Role = int.Parse(this.ddlRole.SelectedValue);
        return admin;
    }
    #endregion
}
